// 二分查找

let search = function (nums, target) {
  let mid, left = 0, right = nums.length - 1;
  while (left <= right) {
    // 取到中位值，若是偶数，为中间2个的第一个；若是奇数，为中间值(这个写法后续可以借鉴)；
    mid = left + ((right - left) >> 1);
    if (nums[mid] > target) {
      right = mid - 1; // 改变区间
    } else if (nums[mid] < target) {
      left = mid + 1; // 改变区间
    } else {
      return mid;
    }
  }
  return -1;
};

let idx = search([-1, 0, 3, 5, 9, 12], 9);
console.log(idx);